package com.hkts.naturalgas.login.controller;

import com.hkts.naturalgas.login.cache.CacheVo;
import com.hkts.naturalgas.login.cache.LoginCacheUtils;
import com.hkts.naturalgas.utils.Result;
import com.hkts.naturalgas.utils.ResultUtil;
import com.hkts.naturalgas.workshop.entity.HktsWorkShop;
import com.hkts.naturalgas.workshop.service.IHktsWorkShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.UUID;

/**
 * @ClassNameLoginController
 * @Description
 * @Author yck
 * @Date2021-12-08 9:47
 * @Version V1.0
 **/
@RestController
@RequestMapping("/enviroment")
public class LoginController {
    @Autowired
    public IHktsWorkShopService iHktsWorkShopService;
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/login")
    public Result<String> login(String workShopName,String passWord) {
       //@RequestBody HktsWorkShop hktsWorkShop
        HktsWorkShop hktsWorkShop=new HktsWorkShop();
        hktsWorkShop.setWorkShopName(workShopName);
        hktsWorkShop.setWorkShopCode(passWord);
        HktsWorkShop be= iHktsWorkShopService.checkForExist(hktsWorkShop);
       if(be==null){
           return  ResultUtil.error("输入的车间名称不存在！");
       }else if(be.getPassWord().equals(hktsWorkShop.getPassWord())){
           return  ResultUtil.error("密码错误！");
       }else{
           String  token = UUID.randomUUID().toString().replace("-", "");
           CacheVo vo=new CacheVo();
           Date date = new Date();
           vo.setLoginTime(date);
           vo.setTimeStamp(date.getTime());
           vo.setWorkShopName(hktsWorkShop.getWorkShopName());
           LoginCacheUtils.map.put(token,vo);
           return ResultUtil.data(token);
       }
    }
    @ResponseBody
    @RequestMapping( value = "/unlogin")
    public Result<String> unlogin(){
        return ResultUtil.error(401,"请重新登录");
    }



}
